package com.oren.Thread;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wsw
 * @time 2018/7/9 21:11
 * @description 手写theadLocal
 * @Since jdk1.7
 */
public class MyThreadLocal<T> {

	private Map<Thread,T> container= Collections.synchronizedMap(new HashMap<Thread, T>());

	//设置值
	public void set(T value){
		container.put(Thread.currentThread(),value);
	}

	public T get(){
		Thread thread=Thread.currentThread();
		T value=container.get(thread);
		if(value==null && !container.containsKey(thread)){
			value= initailValue();
			container.put(thread,value);
		}
		return value;
	}

	public void remove(){
		container.remove(Thread.currentThread());
	}

	protected T initailValue(){
		return null;
	}

}
